package controller

import (
	"gorm.io/gorm"
	"vueProject/globalData"
	"vueProject/models/sys_db"
)

type DbConnect struct {
	ConnectStr string `json:"ConnectStr"`
	DbType     string `json:"DbType"`
	Remarks    string `json:"Remarks"`
}

// 组返回信息格式
func returnMsg(result *gorm.DB) interface{} {
	if result.Error != nil {
		return map[string]interface{}{
			"code": -1,
			"msg":  result.Error,
		}
	}
	return map[string]interface{}{
		"code": 200,
		"msg":  "Ok",
	}
}

// CreateConnect 创建数据库连接配置
// func CreateConnect(Connect DbConnect) interface{} {
func CreateConnect(Connect sys_db.DbConnectList) interface{} {
	//result := globalData.LocalDb.Create(&sys_db.DbConnectList{ConnectStr: Connect.ConnectStr, DbType: Connect.DbType, Remarks: Connect.Remarks})
	result := globalData.LocalDb.Save(&Connect)
	return returnMsg(result)
}

// DeleteConnect 删除数据库连接配置
func DeleteConnect(ID int64) interface{} {
	result := globalData.LocalDb.Unscoped().Delete(&sys_db.DbConnectList{}, ID)
	return returnMsg(result)
}

// GetConnectList 获取全部连接配置项
func GetConnectList() []sys_db.DbConnectList {
	var ConnectList []sys_db.DbConnectList
	globalData.LocalDb.Find(&ConnectList)
	return ConnectList
}
